#!/usr/bin/env node

function urlOf(name, is_org=false) {
		var url = "https://gitee.com/api/v5" + (is_org ? "/orgs/"+name : "/user")
		return url
}

function getEnvOf(name) {
	return process.env[name]
}

function getAccessToken() {
	return getEnvOf('gitee_token')
}

function jsonOfResponse(response, callback) {
	let dt = ''    
	
	// read data buffer with Buffer type
	// var buff = new Buffer(parseInt(size))
	// var offset = 0

	response.on("data", (data) => {
        dt += data
    }).on('end', () => {
        var json = JSON.parse(dt)
        callback(json)
    })
}

function config(argv) {
	let offset = 2
	var name, owner;
	switch(argv.length) {
		case 1 + offset:
		console.log("name: " + argv[0 + offset])
		name = argv[0 + offset]
		return { type:"user", name: name }
		case 2 + offset:
		console.log("owner: "+ argv[0 + offset] + ", name: " + argv[1 + offset])
		name = argv[0 + offset]
		owner = argv[1 + offset]
		return { type:"orgs", owner: owner, name: name }

		default:
        console.log("Usage:")
        console.log("\tsub-command ")
		console.log("\tsub-command projectName")
        console.log("\tsub-command owner projectName")
        return { type:"unknown" }   
	}
}

exports.config = config(process.argv)
exports.getAccessToken = getAccessToken
exports.jsonOfResponse = jsonOfResponse
exports.getEnvOf = getEnvOf
exports.urlOf = urlOf
